package com.eshealthy.serviceImpl;

import com.eshealthy.dao.PatientDao;
import com.eshealthy.entity.*;
import com.eshealthy.service.PatientService;
import com.eshealthy.utils.EsCase;
import com.eshealthy.utils.Week;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.io.IOException;
import java.sql.Timestamp;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.List;

@Service
@Transactional
public class PatientServiceImpl implements PatientService {
    @Autowired
    private PatientDao patientDao;
    //医生登录
    @Override
    public Patient login(Patient patient) {
        Patient patientDB=patientDao.findByTel(patient.getTel());
        if(!ObjectUtils.isEmpty(patientDB)){
            if(patientDB.getPassword().equals(patient.getPassword())){
                return patientDB;
            }else{
                throw new RuntimeException("用户密码输入不正确");
            }
        }else{
            throw new RuntimeException("用户输入错误");
        }

    }
//医生注册
    @Override
    public void register(Patient patient) {
        Patient patientDB=patientDao.findAllPatient(patient.getTel());
        if(patientDB==null){
            try{
                patientDao.save(patient);
            }catch(Exception e){
                throw new RuntimeException("输入错误");
            }

        }else{
            throw new RuntimeException("用户已存在");
        }

    }

    @Override
    public List<PatientSchedule> find(Integer page,Integer rows,Integer id) {
        int start=(page-1)*rows;
        return patientDao.find(start,rows,id);
    }

    @Override
    public void addSchedule(String pid, String did,String schedule) {
        Date date = new Date();
        Week week=new Week();
        System.out.println(week.getDayOfWeek(week.dateToWeek(schedule),0));
        if(week.getDayOfWeek(week.dateToWeek(schedule),0).compareTo(date)==-1){
            System.out.println(week.getDayOfWeek(week.dateToWeek(schedule),0));
            //uilt date转sql date
            patientDao.addSchedule(pid,did,week.getDayOfWeek(week.dateToWeek(schedule),1));
        }else{

            patientDao.addSchedule(pid,did,week.getDayOfWeek(week.dateToWeek(schedule),0));
        }

//        patientDao.addSchedule(pid,did,);
    }

    @Override
    public Integer findTotalsSchedule(Integer id) {
        return patientDao.findTotalsSchedule(id);
    }

    @Override
    public List<Cases> findCases(Integer page, Integer rows, String pid) throws IOException {
        EsCase esCase=new EsCase();
        return esCase.Cases(page,rows,pid);
    }

    @Override
    public Integer findTotalsCase(String pid) throws IOException {
        EsCase esCase=new EsCase();
        return (int)esCase.countPatient(pid);
    }
}

